<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="80px"
      :inline="false"
      size="samll"
    >
      <el-form-item label="导航标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="导航链接" prop="url">
        <el-input v-model="form.url"></el-input>
      </el-form-item>
      <el-form-item label="导航类型" prop="type">
        <el-select v-model="form.type" clearable filterable>
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="导航排序" prop="sort">
        <el-input-number :min="0" v-model="form.sort"></el-input-number>
      </el-form-item>
      <el-form-item label="是否推荐" prop="recommend">
        <el-radio v-model="form.recommend" :label="0">不推荐</el-radio>
        <el-radio v-model="form.recommend" :label="1">推荐</el-radio>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import guideApi from "@/api/guide";
export default {
  name: "guideSave",
  props: {
    guideId: {
      type: String,
      default: "",
    },
    close: {
      type: Boolean,
      defalut: false,
    },
  },
 
  data() {
    return {
      options: [
        { value: "", label: "-请选择-" },
        { value: "1", label: "应用导航" },
        { value: "2", label: "文档导航" },
        { value: "3", label: "文章导航" },
      ],
      form: {
        id: "",
        title: "",
        url: "",
        type: "",
        sort: 0,
        recommend: 0,
      },
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        url: [{ required: true, message: "地址不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "change" }],
      },
    };
  },
   watch: {
    close(newValue, oldValue) {
      this.form = {
        id: "",
        title: "",
        url: "",
        type: "",
        sort: 0,
        recommend: 0,
      };
    },
    guideId(newValue, oldValue){
      if (this.guideId) this.getInfo(this.guideId);
    }
  },
  created() {
    if (this.guideId) this.getInfo(this.guideId);
  },
  methods: {
    // 获取数据
    async getInfo(id) {
      let res = await guideApi.info(id);
      if (res.success) {
        this.form = res.data.data;
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 保存
    onSubmit() {
      this.$refs.form.validate((v) => {
        if (this.guideId) {
          this.handleUpdate();
        } else {
          this.handleAdd();
        }
      });
    },
    // 添加
    async handleAdd() {
      let res = await guideApi.add(this.form);
      if (res.success) {
        this.$message.success("添加成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 修改
    async handleUpdate() {
      let res = await guideApi.update(this.form);
      if (res.success) {
        this.$message.success("修改成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },

    handleClose() {
      this.$emit("done");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>